//
//  SADiscountSetVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/6/21.
//

#import "SADiscountSetVC.h"
#import "UILabel+NSMutableAttributedString.h"
#import "SASystemSettingsListAPI.h"
#import "SASystemSettingsCheckAPI.h"

@interface SADiscountSetVC ()<HTTPAPIDelegate>

@property (nonatomic, strong) UILabel *tipLab;
@property (nonatomic, strong) UILabel *tipLab2;
@property (nonatomic, strong) UIButton *switchBtn;
@property (nonatomic, strong) SASystemSettingsListAPI *systemSettingsListAPI;
@property (nonatomic, strong) SASystemSettingsCheckAPI *systemSettingsCheckAPI;

@property (nonatomic, strong) NSString *settingsId;
@property (nonatomic, strong) NSString *value;

@end

@implementation SADiscountSetVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.systemSettingsListAPI == api) {
        self.settingsId = data[@"data"][@"discount_setting"][@"settingsId"];
        self.value = data[@"data"][@"discount_setting"][@"value"];
        self.switchBtn.selected = [self.value isEqualToString:@"1"];
    }else if (self.systemSettingsCheckAPI == api) {
        
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {

}

-(void)clickedSwitchBtn:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    if (sender.isSelected) {
        self.systemSettingsCheckAPI.parentId = @"";
        self.systemSettingsCheckAPI.value = @"1";
        self.systemSettingsCheckAPI.settingsId = self.settingsId;
        [self.systemSettingsCheckAPI sendRequestToServerWithActivityViewAndMask:POST];
    }else {
        self.systemSettingsCheckAPI.parentId = @"";
        self.systemSettingsCheckAPI.value = @"0";
        self.systemSettingsCheckAPI.settingsId = self.settingsId;
        [self.systemSettingsCheckAPI sendRequestToServerWithActivityViewAndMask:POST];
    }
}

-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@"折扣设置"];
    [self setBgImage:nil];
    self.view.backgroundColor = kBgColor;
    
    UIView *whiteView = [[UIView alloc] initWithFrame:SARectMake(0, StatusHeight, kBaseScreenWidth, 40)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteView];
    
    [whiteView addSubview:self.tipLab];
    [whiteView addSubview:self.switchBtn];
    [self.view addSubview:self.tipLab2];
    
    [self.tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(0);
    }];
    
    [self.tipLab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.equalTo(whiteView.mas_bottom).offset(kScaleNum(5));
    }];
    
    [self.switchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(kScaleNum(84));
    }];
    
    [self.tipLab2 setAttributedStringWithSubString:@"*" color:[UIColor redColor]];
    
    [self.systemSettingsListAPI sendRequestToServerWithActivityViewAndMask:GET];
    
}

- (UILabel *)tipLab {
    if (!_tipLab) {
        _tipLab = [[UILabel alloc] init];
        _tipLab.font = kFont(15);
        _tipLab.text = @"会员折扣和商品折扣叠加使用";
        _tipLab.textColor = kBlackColor;
    }
    return _tipLab;
}

- (UILabel *)tipLab2 {
    if (!_tipLab2) {
        _tipLab2 = [[UILabel alloc] init];
        _tipLab2.font = kFont(12);
        _tipLab2.text = @"*如不叠加,会员折扣和商品折扣取最低折扣";
        _tipLab2.textColor = kLightgGrayColor;
    }
    return _tipLab2;
}

-(UIButton *)switchBtn {
    if (!_switchBtn) {
        _switchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_switchBtn setImage:[UIImage imageNamed:@"gray_switch"] forState:UIControlStateNormal];
        [_switchBtn setImage:[UIImage imageNamed:@"green_switch"] forState:UIControlStateSelected];
        [_switchBtn addTarget:self action:@selector(clickedSwitchBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchBtn;
}

-(SASystemSettingsListAPI *)systemSettingsListAPI {
    if (!_systemSettingsListAPI) {
        _systemSettingsListAPI = [SASystemSettingsListAPI apiWithDelegate:self];
    }
    return _systemSettingsListAPI;
}

-(SASystemSettingsCheckAPI *)systemSettingsCheckAPI {
    if (!_systemSettingsCheckAPI) {
        _systemSettingsCheckAPI = [SASystemSettingsCheckAPI apiWithDelegate:self];
    }
    return _systemSettingsCheckAPI;
}

@end
